package com.orhon.smartcampus.modules.material.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.material.dao.BuildingsDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Site;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLSite;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.SiteMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class BuildingListFetcher implements DataFetcher {

    @Autowired
    BuildingsDao buildingsDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        List<GQLBuilding> GQLBuilding = new ArrayList<>();


        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
//        DetachedCriteria criteria = DetachedCriteria.forClass(Buildings.class);
//        criteria.add(Restrictions.isNull("deletedAt"));
//        List<Buildings> list = (List<Buildings>) buildingsDao.findByCriteria(criteria,page.getPage(),page.getLimit());
        String hql = "from Buildings where deleted_at = null and school_id = " + schoolId;

        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("id")!=null) {
                hql += "and id " + Util.decide(map.get("id"));
            }
            if(map.get("code")!=null) {
                hql += "and buildings_code  " +Util.decide( map.get("code"));
            }
            if(map.get("campus_id")!=null) {
                hql += "and  campus_id " + Util.decide(map.get("campus_id"));
            }
            if(map.get("site_id")!=null) {
                hql += "and  site_id " + Util.decide(map.get("site_id"));
            }
            if(map.get("school_id")!=null) {
                hql += "and  school_id " + Util.decide(map.get("school_id"));
            }

            if(map.get("unit_num")!=null) {
                hql += "and  unit_num " + Util.decide(map.get("unit_num"));
            }

            if(map.get("floor_num")!=null) {
                hql += "and  floor_num " + Util.decide(map.get("floor_num"));
            }

            if(map.get("status")!=null) {
                hql += "and  status " + Util.decide(map.get("status"));
            }

        }
		List<Buildings> list = buildingsDao.find(hql,null,page.getPage(),page.getLimit());

        for (Buildings item : list) {
//            GQLSchool gqlSchool = null;
//            if (env.getSelectionSet().contains("data/school")){
//                Schools schools = item.getBaseSchoolsBySchoolId();
//                if (schools !=null && schools.getDeleted_at() == null){
//                    gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
//                }
//            }
            GQLSite gqlSite = null;
            if (env.getSelectionSet().contains("data/site")){
                Site site = item.getSite();
                if (site !=null && site.getDeletedAt() == null) {
                    gqlSite = SiteMapper.MAPPER.toSite(site);
                }
            }
//            GQLCampus gqlCampus = null;
//            if (env.getSelectionSet().contains("data/campus")){
//                Campus campus = item.getMaterialCampusByCampusId();
//                if (campus !=null && campus.getDeletedAt() == null) {
//                    gqlCampus = CampusMapper.MAPPER.toCampus(campus);
//                }
//            }
            GQLBuilding building = BuildingMapper.MAPPER.toBuilding(item);
//            building.setSchool(gqlSchool);
//            building.setCampus(gqlCampus);
            building.setSite(gqlSite);
            GQLBuilding.add(building);
        }

        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
            total = buildingsDao.getTotalCount(hql);
        }
        return Result.ok().put("data", GQLBuilding).put("total", total);
    }
}
